package com.augn.service.impl;

import com.augn.entity.Goods;
import com.augn.entity.Result;
import com.augn.entity.User;
import com.augn.mapper.GoodsMapper;
import com.augn.service.IGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private GoodsMapper mapper;

    // 新增一件商品
    @Override
    public Result<?> insertOneGoods(Goods goods) {
        int insert = mapper.insert(goods);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    //删除一件商品
    @Override
    public Result<?> deleteOneGoods(Integer id) {
        int delete = mapper.deleteById(id);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    //更新一件商品
    @Override
    public Result<?> updateOneGoods(Goods goods) {
        int update = mapper.updateById(goods);
        if(update==0){
            return Result.error("-1","更新失败");
        } else{
            return Result.success(update);
        }
    }

    //获取一件商品
    @Override
    public Result<?> getOneGoods(Integer id) {
        Goods goods = mapper.selectById(id);
        if(goods==null){
            return Result.error("-1","无此商品");
        } else{
            return Result.success(goods);
        }
    }

    //获取符合三种id及符合keyword的一条件或者符合任意两条件或者任意三条件或同时符合四条件的商品
    @Override
    public Result<?> getPartGoods(Goods goods,Integer pageNum,Integer pageSize) {
        QueryWrapper<Goods> wrapper= new QueryWrapper<>();
        if(goods.getGoodsId()!=null){
            wrapper.eq("goods_id",goods.getGoodsId());
        }
        if (goods.getCulId()!=null) {
            wrapper.eq("cul_id",goods.getCulId());
        }
        if (goods.getManuId()!=null) {
            wrapper.eq("manu_id",goods.getManuId());
        }
        if (goods.getAuthorize()!=null) {
            wrapper.eq("authorize",goods.getAuthorize());
        }
        if (goods.getKeyword()!=null) {
            wrapper.and(i ->i.like("keyword",goods.getKeyword())
                    .or().like("sort",goods.getKeyword())
                    .or().like("introduction",goods.getKeyword()));
        }

        long pages ,total;
        List<Goods> goodsList;
        Map<String,Object> ret = new HashMap<>();
        if((pageNum==null&&pageSize==null)||(pageNum==null&&pageSize!=null)){
            goodsList =  mapper.selectList(wrapper);
            ret.put("goodsList",goodsList);
        } else {
            if(pageSize==null){
                pageSize=20;
            }
            Page<Goods> goodsPage = new Page<>(pageNum , pageSize);
            IPage<Goods> goodsIPage = mapper.selectPage(goodsPage , wrapper);
            goodsList = goodsIPage.getRecords();
            pages = goodsIPage.getPages();
            total = goodsIPage.getTotal();
            ret.put("goodsList",goodsList);
            ret.put("pages",pages);
            ret.put("total",total);
        }

        if(goodsList.isEmpty()){
            return Result.error("-1","没有符合此条件的商品");
        } else{
            return Result.success(ret);
        }
    }
}
